﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZK.MyControl
{
    public partial class MyNumTrackBar : UserControl
    {
        public MyNumTrackBar()
        {
            InitializeComponent();
        }


        public delegate void ChangeHandle(int value);

        public event ChangeHandle ValueChangeHandle;


     
        public NumericUpDown NumericUpDown
        {

            get {
                return  this.numUpDown;

            }
            set {
                this.numUpDown = value;
            }
        }


        public TrackBar TrackBar
        {

            get
            {
                return this.trackBar;

            }
            set
            {
                this.trackBar = value;
            }
        }



        private int _value = 0;
        public int Value {

            get
            {

                if (_value < _minimum)
                {
                    _value = _minimum;
                }
                if (_value > _maximum)
                {
                    _value = _maximum;
                }
                return  _value;
            }
            set
            {
                

               _value = value;
 
                if (_value < numUpDown.Minimum)
                {
                    numUpDown.Value = numUpDown.Minimum;
                }

                else if (_value > numUpDown.Maximum)
                {
                    numUpDown.Value = numUpDown.Maximum;
                }
                else {

                    numUpDown.Value = _value;
                }


                if (_value < trackBar.Minimum)
                {
                    trackBar.Value = trackBar.Minimum;
                }

                else if (_value > trackBar.Maximum)
                {
                    trackBar.Value = trackBar.Maximum;
                }
                else
                {

                    trackBar.Value = _value;
                }

              

                if (this.ValueChangeHandle != null)
                {
                    this.ValueChangeHandle(_value);
                }
            }

        }

        private int _minimum = 0;
        public int Minimum {

            get {
                return _minimum;
            }

            set {
                _minimum = value;
                numUpDown.Minimum = _minimum;
                trackBar.Minimum = _minimum;
            }
        }
        private int _maximum = 100;
        public int Maximum
        {

            get
            {
                return _maximum;
            }

            set
            {
                _maximum = value;
                numUpDown.Maximum = _maximum;
                trackBar.Maximum = _maximum;
            }
        }



        private void btnReset_Click(object sender, EventArgs e)
        {
            numUpDown.Value = numUpDown.Minimum;

            Value = (int)numUpDown.Value;
        }

        private void numUpDown_ValueChanged(object sender, EventArgs e)
        {
            int val = (int)numUpDown.Value;
            trackBar.Value = val;

            Value = val;
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            numUpDown.Value = trackBar.Value;
            numUpDown.Refresh();
        }
    }
}
